
import requests

# 通过获取信息，创建助手功能
class KingOfGloryAssistant():
    # 初始化
    def __init__(self):
        self.req = requests
        self.response = ""
        self.head = {'Accept-Charset': 'UTF-8',
            'Accept-Encoding': 'gzip,deflate',
            'User-Agent': 'Dalvik/2.1.0 (Linux; U; Android 6.0.1; MI 5 MIUI/V8.1.6.0.MAACNDI)',
            'X-Requested-With': 'XMLHttpRequest',
            'Content-type': 'application/x-www-form-urlencoded',
            'Connection': 'Keep-Alive',
            'Host': 'gamehelper.gm825.com'}

        self.hero_list_url = "http://gamehelper.gm825.com/wzry/hero/list?channel_id=90009a&app_id=h9044j&game_id=7622&game_name=%E7%8E%8B%E8%80%85%E8%8D%A3%E8%80%80&vcode=12.0.3&version_code=1203&cuid=2654CC14D2D3894DBF5808264AE2DAD7&ovr=6.0.1&device=Xiaomi_MI+5&net_type=1&client_id=1Yfyt44QSqu7PcVdDduBYQ%3D%3D&info_ms=fBzJ%2BCu4ZDAtl4CyHuZ%2FJQ%3D%3D&info_ma=XshbgIgi0V1HxXTqixI%2BKbgXtNtOP0%2Fn1WZtMWRWj5o%3D&mno=0&info_la=9AChHTMC3uW%2BfY8%2BCFhcFw%3D%3D&info_ci=9AChHTMC3uW%2BfY8%2BCFhcFw%3D%3D&mcc=0&clientversion=&bssid=VY%2BeiuZRJ%2FwaXmoLLVUrMODX1ZTf%2F2dzsWn2AOEM0I4%3D&os_level=23&os_id=dc451556fc0eeadb&resolution=1080_1920&dpi=480&client_ip=192.168.0.198&pdunid=a83d20d8"
        self.hero_info_url = "http://gamehelper.gm825.com/wzry/hero/detail?hero_id={}&channel_id=90009a&app_id=h9044j&game_id=7622&game_name=%E7%8E%8B%E8%80%85%E8%8D%A3%E8%80%80&vcode=12.0.3&version_code=1203&cuid=2654CC14D2D3894DBF5808264AE2DAD7&ovr=6.0.1&device=Xiaomi_MI+5&net_type=1&client_id=1Yfyt44QSqu7PcVdDduBYQ%3D%3D&info_ms=fBzJ%2BCu4ZDAtl4CyHuZ%2FJQ%3D%3D&info_ma=XshbgIgi0V1HxXTqixI%2BKbgXtNtOP0%2Fn1WZtMWRWj5o%3D&mno=0&info_la=9AChHTMC3uW%2BfY8%2BCFhcFw%3D%3D&info_ci=9AChHTMC3uW%2BfY8%2BCFhcFw%3D%3D&mcc=0&clientversion=&bssid=VY%2BeiuZRJ%2FwaXmoLLVUrMODX1ZTf%2F2dzsWn2AOEM0I4%3D&os_level=23&os_id=dc451556fc0eeadb&resolution=1080_1920&dpi=480&client_ip=192.168.0.198&pdunid=a83d20d8"
        self.weapon_url = "http://gamehelper.gm825.com/wzry/equip/list?channel_id=90009a&app_id=h9044j&game_id=7622&game_name=%E7%8E%8B%E8%80%85%E8%8D%A3%E8%80%80&vcode=12.0.3&version_code=1203&cuid=2654CC14D2D3894DBF5808264AE2DAD7&ovr=6.0.1&device=Xiaomi_MI+5&net_type=1&client_id=1Yfyt44QSqu7PcVdDduBYQ%3D%3D&info_ms=fBzJ%2BCu4ZDAtl4CyHuZ%2FJQ%3D%3D&info_ma=XshbgIgi0V1HxXTqixI%2BKbgXtNtOP0%2Fn1WZtMWRWj5o%3D&mno=0&info_la=9AChHTMC3uW%2BfY8%2BCFhcFw%3D%3D&info_ci=9AChHTMC3uW%2BfY8%2BCFhcFw%3D%3D&mcc=0&clientversion=&bssid=VY%2BeiuZRJ%2FwaXmoLLVUrMODX1ZTf%2F2dzsWn2AOEM0I4%3D&os_level=23&os_id=dc451556fc0eeadb&resolution=1080_1920&dpi=480&client_ip=192.168.0.198&pdunid=a83d20d8"
        # 保存英雄列表ID
        self.hero_id_list = []

    def getHeroList(self):
        # 1 发起请求
        self.response = self.req.get(url=self.hero_list_url,headers=self.head)
        #print("code-",self.response.status_code)
        # print(type(self.response.text))
        # 2 使用json()函数处理返回的json数据，转换成json对象，符合python基本数据类型
        data = self.response.json()
        #print(type(data))
        # 3 获取json内的英雄列表
        hero_list = data["list"]
        # 4 格式化输出英雄数量
        print("王者荣耀英雄总数量：%d"%(len(hero_list)))
        # 5 迭代器：格式化输出英雄的id、name
        flag = 0
        for hero in hero_list:
            print("%s的ID：%s"%(hero["name"],hero["hero_id"]),end="\t\t")
            # 每累加3次，跨行处理
            flag += 1
            if flag == 3:
                print("\n")
                flag = 0
            # 保存
            self.hero_id_list.append(hero["hero_id"])



    def getHeroInfo(self):
        print("\n")
        heri_id = input("请通过ID选择英雄:")
        if heri_id in self.hero_id_list:
            # 1 发起请求
            # format格式化字符串
            self.response = self.req.get(url=self.hero_info_url.format(heri_id), headers=self.head)
            print("url-",self.response.url)
            print("code-",self.response.status_code)
            # 2 格式化JSON
            data = self.response.json()
            print("英雄姓名：\t%s"%(data["info"]["name"]))
            print("英雄标题：\t%s" % (data["info"]["title"]))
            print("英雄杀人介绍：\t%s" % (data["info"]["skill_tips"]),end="\n")
            # 3 技能介绍
            for item in data["info"]["skill_list"]:
                print("\t英雄技能：\t%s" % (item["name"]))
                print("\t英雄技能描述：\t%s" % (item["description"]))
                print("\t英雄技能介绍：\t%s" % (item["intro"]))
                print("\t英雄技能类型：\t%s" % (item["tags"]),end="\n\n")

            # 4 推荐出装
            # 5 请求武器信息
            weapon = self.getHeroWeapon()
            # 6 迭代器：遍历推荐神装
            for equip_choice in data["info"]["equip_choice"]:
                print("\t推荐系列：%s"%(equip_choice["title"]))
                print("\t推荐介绍：%s" % (equip_choice["description"]))
                # 7 遍历具体额武器信息
                for equip in equip_choice["list"]:

                    # 8 通过武器ID匹配商店武器信息
                    weapon_name, weapon_price = self.getHeroWeaponInfo(equip["equip_id"],weapon)
                    print("\t装备名称：%s"%(weapon_name))
                    print("\t装备价钱：%s" % (weapon_price))

                print("\n")

        else:
            print("请输入正确的ID")
            self.getHeroInfo()


    def getHeroWeapon(self): # 获取装备商店信息
        self.response = self.req.get(url=self.weapon_url, headers=self.head)
        return self.response.json()["list"]

    def getHeroWeaponInfo(self,equip_id,weapon): # 通过武器ID匹配商店武器信息
        for w in weapon:
            # 1 判断：推荐武器id是否等于武器商店内的id
            if equip_id == w["equip_id"]:
                weapon_name =  w["name"]
                weapon_price = w["price"]
                # 2 同时两个变量：在调用位置也需要声明两个变量保存返回值
                return weapon_name,weapon_price


    # 入口
    def main(self):
        # 主界面
        print("*"*15,"王者荣耀小助手","*"*15)
        print("*" * 17, "查询英雄", "*" * 18)
        print("*" * 17, "推荐出装", "*" * 18)
        print("*" * 19, "盗版", "*" * 19)


if __name__ == '__main__':
    koga = KingOfGloryAssistant()
    koga.main()
    koga.getHeroList()
    koga.getHeroInfo()


























